//
//  CYJDDetail.m
//  CreditAddressBook
//
//  Created by mosu on 2016/11/4.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYJDDetail.h"
#import "CYUser.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYDES3Util.h"
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import <ZMCreditSDK/ALCreditService.h>
#import "DCPaymentView.h"


@interface CYJDDetail ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *usernameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (nonatomic, strong) UITextField *dealTF;

@property (nonatomic, strong)UIWebView *webview;

@end

@implementation CYJDDetail

- (void)viewDidLoad {
     _passwordTF.secureTextEntry = YES;
     self.view.backgroundColor = CYColor(147, 197, 83, 1);
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)submitClick:(id)sender {
     if (_usernameTF.text.length == 0) {
          CYAlertView(@"请输入京东账号");
          return;
     }
     if (_passwordTF.text.length == 0) {
          CYAlertView(@"请输入京东密码");
          return;
     }
     
     DCPaymentView *payAlert = [[DCPaymentView alloc]init];
     payAlert.detail = @"请输入交易密码";
     [payAlert show];
     payAlert.completeHandle = ^(NSString *inputPwd) {
          NSString *desstr = [CYDES3Util encrypt:inputPwd];
          CYLog(@"desstr = %@",desstr);
          CYUser *user = [CYLoginHttpTool userData].user;
          [MBProgressHUD showMessage:nil toView:self.view];
          _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 30, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
          _webview.backgroundColor = CYColor(147, 197, 83, 1);;
          _webview.delegate = self;
          if (user) {
               [[self navigationController] setNavigationBarHidden:YES animated:NO];
               //http://192.168.0.172:8058   http://www.credit-manage.com:8058 http://192.168.1.114:8058
               NSString *urlstr = [NSString stringWithFormat:@"http://www.credit-manage.com:8058/phone/apix/saveJuHeJingDongFirst.htm?userId=%@&token=%@&login_name=%@&passwd=%@&dealCode=%@",user.userId,user.token,_usernameTF.text,_passwordTF.text,desstr];
               NSString *strUrl = [urlstr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
               CYLog(@"strUrl = %@",strUrl);
               
               NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
               [_webview loadRequest:request];
               [self.view addSubview:_webview];
          }

          
//          [CYHttpTool postWithURL:[CYURL shared].saveJuHeJingDongFirst params:@{@"userId":user.userId,@"token":user.token,@"username":_usernameTF.text,@"password":_passwordTF.text,@"dealCode":desstr} success:^(id json) {
//               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//               //
//               [MBProgressHUD hideHUDForView:self.view animated:YES];
//               NSString *data = jsoninfo.data;
//               if (data && data.intValue == 0) {
//                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入您所获取到的验证码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//                    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
//                    [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//                         if (buttonIndex == 1) {
//                              _dealTF = [alert textFieldAtIndex:0];
//                              [CYHttpTool postWithURL:[CYURL shared].saveJuHeJingDongSecond params:@{@"userId":user.userId,@"token":user.token,@"smsCode":_dealTF.text} success:^(id json) {
//                                   [MBProgressHUD hideHUDForView:self.view animated:YES];
//                                   CYJsonInfo *jsoninfo1 = [CYJsonInfo objectWithKeyValues:json];
//                                   CYAlertView(jsoninfo1.message);
//                                   [self.navigationController popViewControllerAnimated:YES];
//                                   
//                              } failure:^(NSError *error) {
//                                   [MBProgressHUD hideHUDForView:self.view animated:YES];
//                              }];
//                              
//                         }
//                    }];
//                    
//               }else{
//                    CYAlertView(jsoninfo.message);
//                    [self.navigationController popViewControllerAnimated:YES];
//                    [MBProgressHUD hideHUDForView:self.view animated:YES];
//                    
//               }
//               
//          } failure:^(NSError *error) {
//               [MBProgressHUD hideHUDForView:self.view animated:YES];
//               
//          }];
          
     };
     
}

#pragma mark -
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
          if (buttonIndex == 0) {
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
     }];
     
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     //[self.view addSubview:_hiview];
     //[MBProgressHUD hideHUDForView:self.view];
     NSString *url = [[request URL] absoluteString];
     NSLog(@"url = %@",url);
     //h5内返回控件字段方法:loanlist
     if([url hasSuffix:@"loanlist"]) {
          dispatch_async(dispatch_get_main_queue(), ^{
               //返回上一级时，取消nav的隐藏
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               [self dismissViewControllerAnimated:YES completion:^{
                    //do what you want
                    
               }];
               //js交互oc获取到h5内的返回按钮后，点击返回，popvc
               [self.navigationController popViewControllerAnimated:YES];
          });
          return NO;
     }
     
     return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
